<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BScreenJoinService
 *
 * @author jianfeng
 */
class BScreenJoinService extends BUIScreen {

    //put your code here

    private $senderPermits = array();
    private $receiverPermits = array();
    public $joinServiceModel;

    public function render($config = null, $style = NULL) {
        //parent::render();
        
        $activeServiceUI = Blyn::app()->getActiveService()->getUIManager();
     
        //remove serviceTopBar 
        if(isset($activeServiceUI->views['serviceTopBar']))
            unset($activeServiceUI->views['serviceTopBar']);
        
        $activeAppUI = Blyn::app()->getActiveServiceApp()->getUIManager();
        
        //remove serviceAppTopBar
        if(isset($activeAppUI->views['serviceAppTopBar']))
            unset($activeAppUI->views['serviceAppTopBar']);

        $this->joinServiceModel = new JoinServiceFormModel();
        if (isset($_POST['JoinServiceFormModel'])) {
            $this->joinServiceModel->attributes = $_POST['JoinServiceFormModel'];
            //$screen->renderView('leftMenu');
        }

        //means joinServiceBtn is clicked
        if ($this->isAjaxLinkClicked('joinServiceBtn')) {
            $addingServiceId = $_POST['joined_serviceId'];
            $senderMessage = $_POST['join_service_message'];
            $applyingRole = $_POST['applyingRole'];
            $this->sendJoinServiceRequest($addingServiceId, $senderMessage, $applyingRole);
            //because service list in left menu changed, so need reload services of roletype
            Blyn::app()->getCurrentUser()->resetTransitVariable(BUser::TransitVariable_UserServicesOfRoleType);            
            //add runtime views for ajax link
            $this->addAjaxRenderView('joinServiceBtn', 'leftMenu');
            //manually call startRender to start render
            $this->startRender();
        }        

        $this->addRenderView('joinService', 'joinService', array('model' => $this->joinServiceModel, 'screen' => $this));
    }

    public function findServices($searchText) {

        $opContractData = $this->getOperationContract();
        $opContract = $opContractData['operationContract'];
        $myRole = $opContractData['myRole'];

        $qualifiedServices = array();

        $services = BService::findServices();

        //$user = Blyn::app()->getCurrentUser();

        foreach ($services as $service) {

            if (strpos($myRole, 'sender') !== FALSE) {
                $this->senderPermits[$service->getId()] = $opContract->getOperationPermits('joinService', 'senderPermit');
                $this->receiverPermits[$service->getId()] = $opContract->getOperationPermits('joinService', 'receiverPermit');
                if ($opContract->checkSenderPermit('joinService', array('serviceId' => $service->getId())))
                    array_push($qualifiedServices, $service);
            }
        }

        return $qualifiedServices;
    }

    public function sendJoinServiceRequest($serviceId, $senderMessage, $choosedRole) {

        $receiverPermits = array();
        $joinServiceContract = new BOCJoinService(array('serviceId' => $serviceId));

        if ($this->receiverPermits != NULL) {
            $receiverPermits = $this->receiverPermits[$serviceId];
        } else {
            $opContractData = $this->getOperationContract();
            $opContract = $opContractData['operationContract'];
            $receiverPermits = $opContract->getOperationPermits('joinService', 'receiverPermit');
        }

        foreach ($receiverPermits as $role) {
            $result = $joinServiceContract->sendOperationRequest(
                    array('name' => 'joinService',
                'data' => array('senderMessage' => $senderMessage, 'applyingRole' => $choosedRole)), array('role' => $role, 'serviceId' => $serviceId));
        }
    }

    public function getSenderPermits($serviceId = NULL) {

        if ($this->senderPermits == NULL) {
            $opContractData = $this->getOperationContract();
            $opContract = $opContractData['opContract'];
            $myRole = $opContractData['myRole'];
            return $opContract->getOperationPermits('joinService', 'senderPermit');
        }
        if ($serviceId == NULL)
            return $this->senderPermits;
        else {
            return $this->senderPermits[$serviceId];
        }
    }

    private function getOperationContract() {
        $activeServiceApp = Blyn::app()->getActiveServiceApp();
        $operationConfig = $activeServiceApp->getConfigManager()->getConfig('appOperations');
        $joinServiceConfig = $operationConfig['joinService'];

        if (isset($joinServiceConfig['operationContract'])) {
            $class = $joinServiceConfig['operationContract']['class'];
            $opContract = new $class;
            $myRole = $joinServiceConfig['operationContract']['myRole'];
        }

        return array('operationContract' => $opContract, 'myRole' => $myRole);
    }

}

?>
